﻿using System;
using System.Collections.Generic;

namespace HDM.NexusEngine.Core
{
    public class Font
    {
        private Texture _texture;
        private readonly Dictionary<char, CharacterData> _characterData;

        public Font(Texture texture, Dictionary<char, CharacterData> characterData)
        {
            _texture = texture;
            _characterData = characterData;
        }

        public Vector MeasureFont(string text, double maxWidth = -1)
        {
            var dimensions = new Vector();

            foreach (var c in text)
            {
                var data = _characterData[c];
                dimensions.X += data.XAdvance;
                dimensions.Y = Math.Max(dimensions.Y, data.Height + data.YOffset);
            }

            return dimensions;
        }
        
        public CharacterSprite CreateSprite(char c)
        {
            var charData = _characterData[c];
            var sprite = new Sprite {Texture = _texture};

            // Setup UVs
            var topLeft = new Point(charData.X/(float)_texture.Width, charData.Y/(float)_texture.Height);
            var bottomRight = new Point(topLeft.X + (charData.Width/(float)_texture.Width), topLeft.Y + (charData.Height/(float)_texture.Height));
            sprite.SetUVs(topLeft, bottomRight);
            sprite.SetWidth(charData.Width);
            sprite.SetHeight(charData.Height);
            sprite.SetColor(new Color(1,1,1,1));

            return new CharacterSprite(sprite, charData);
        }

        
    }
}
